package love.yuanmengrobot.springmvcdemo2.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import love.yuanmengrobot.springmvcdemo2.mapper.NoticeMapper;
import love.yuanmengrobot.springmvcdemo2.pojo.SysNotice;
import love.yuanmengrobot.springmvcdemo2.pojo.SysUser;
import love.yuanmengrobot.springmvcdemo2.service.NoticeService;
import love.yuanmengrobot.springmvcdemo2.utils.DataGridView;
import love.yuanmengrobot.springmvcdemo2.utils.WebUtils;
import love.yuanmengrobot.springmvcdemo2.vo.NoticeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NoticeServiceImpl implements NoticeService {
//    @Autowired
    @Resource
NoticeMapper noticeMapper;
    @Override
    public DataGridView queryAllNotice(NoticeVo noticeVo) {
        Page<SysNotice> page= PageHelper.startPage(noticeVo.getPage(),noticeVo.getLimit());
        List<SysNotice> list=this.noticeMapper.queryAllNotice(noticeVo);
        return new DataGridView(page.getTotal(),list);
    }

    @Override
    public void addNotice(NoticeVo noticeVo) {
        noticeVo.setCreatetime(new Date());
        SysUser user= (SysUser) WebUtils.getHttpSession().getAttribute("user");
        noticeVo.setOpername(user.getRealname());
        this.noticeMapper.insertSelective(noticeVo);
    }

    @Override
    public void updateNotice(NoticeVo noticeVo) {
        this.noticeMapper.updateByPrimaryKeySelective(noticeVo);
    }

    @Override
    public void deleteNotice(Integer id) {
        this.noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void batchDeleteNotice(Integer[] ids) {
        for (Integer id : ids) {
            this.noticeMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public SysNotice loadNoticeById(Integer id) {
        return this.noticeMapper.selectByPrimaryKey(id);
    }
}
